package stack;

import java.util.LinkedList;

public class EvaluateReversePolishNotation150 {
    public int evalRPN(String[] tokens) {
        LinkedList<String> stack = new LinkedList<>();
        for (int i = 0; i < tokens.length; i++) {
            String s = tokens[i];
            if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
                int num2 = Integer.valueOf(stack.pollLast());
                int num1 = Integer.valueOf(stack.pollLast());
                if(s.equals("+")){
                    stack.offerLast(String.valueOf(num1+num2));
                }
                if(s.equals("-")){
                    stack.offerLast(String.valueOf(num1-num2));
                }
                if(s.equals("*")){
                    stack.offerLast(String.valueOf(num1*num2));
                }
                if(s.equals("/")){
                    stack.offerLast(String.valueOf(num1/num2));
                }
            }else {
                stack.offerLast(s);
            }
        }
        return Integer.valueOf(stack.peek());
    }
}
